﻿using System;
using System.Collections.Generic;

namespace Sinacor.Infra.Core.Licensing.Business
{
    public class Alias
    {
        private Data.Aliases _resourceAccess;
        private object locker = new object();

        public Alias()
        {
            _resourceAccess = new Data.Aliases();
        }

        public bool VerificarAliasExiste(Sinacor.Infra.Core.Licensing.Alias library)
        {
            return _resourceAccess.VerificarAliasExiste(library);
        }

        public Sinacor.Infra.Core.Licensing.Alias SelecionarCodAlias(Sinacor.Infra.Core.Licensing.Alias library)
        {
            return _resourceAccess.SelecionarCodAlias(library);
        }

        public List<Sinacor.Infra.Core.Licensing.Alias> Selecionar(Sinacor.Infra.Core.Licensing.Alias library)
        {
            return _resourceAccess.Selecionar(library);
        }

        public Sinacor.Infra.Core.Licensing.Alias SelecionarEmpresa(Sinacor.Infra.Core.Licensing.Alias library)
        {
            return _resourceAccess.SelecionarAlias(library);
        }
        
        public int Incluir(Sinacor.Infra.Core.Licensing.Alias library)
        {
			library.ID = _resourceAccess.SelecionarProximoIdAlias();
			_resourceAccess.Incluir(library);
			return library.ID.Value;
        }

		public void IncluirNovaCompanhia(Sinacor.Infra.Core.Licensing.Alias library, Sinacor.Infra.Core.Licensing.Company company)
		{
			Company newCompany = new Empresa().Incluir(company);
			_resourceAccess.IncluirNovaCompanhia(library, newCompany);
		}

        public void Alterar(Sinacor.Infra.Core.Licensing.Alias entity)
        {
            _resourceAccess.Alterar(entity);
        }

        public void Excluir(Sinacor.Infra.Core.Licensing.Alias entity)
        {
            _resourceAccess.Excluir(entity);
        }

		public List<Company> SelecionarCompaniasVinculadas(Sinacor.Infra.Core.Licensing.Alias entity)
		{
			return _resourceAccess.SelecionarCompaniasVinculadas(entity);
		}

		public List<Company> SelecionarCompaniasNaoVinculadas(Sinacor.Infra.Core.Licensing.Alias entity)
		{
			return _resourceAccess.SelecionarCompaniasNaoVinculadas(entity);
		}

        int SelecionarProximoIdAlias()
        {
            int nextId = 0;

            lock (locker)
            {
                nextId = _resourceAccess.SelecionarProximoIdAlias();
            }

            return nextId;
        }

		public void AtualizarCompaniasVinculadas(Sinacor.Infra.Core.Licensing.Alias entity)
		{
			_resourceAccess.AtualizarCompaniasVinculadas(entity);
		}
    }
}